package com.antler.smt.admin.controller;

import com.antler.smt.admin.annotation.OperateCut;
import com.antler.smt.admin.mapper.ProductionLogInfoMapper;
import com.antler.smt.entity.*;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.admin.EditOrderStatusReq;
import com.antler.smt.request.base.BasePageReq;
import com.antler.smt.request.portal.OrderLogisticsReq;
import com.antler.smt.response.admin.OrderManageListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.ManageOrderService;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单管理 TODO
 *
 * @author zhangyi
 * @date 2018/4/27 19:08
 */
@RestController
@RequestMapping("/orderManage")
@Api(description = "订单管理")
public class OrderManageController extends BaseController {

    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Resource
    private ManageOrderService manageOrderService;

    @Resource
    private ProductionLogInfoMapper productionLogInfoMapper;

    /**
     * 获取所有订单管理列表实体
     *
     * @param pageReq 用于分页
     * @return Result实体
     */
    @PostMapping("/orderManageList")
    @ApiOperation(value = "分类获取订单信息 全部订单 0,1,2,3,4,6,7,8,100  待对接服务商 0  待付款 1 生产中 2-3 待收货 4 待评价 6 已完成 7-8 驳回 100", httpMethod = "POST")
    public Result<PageInfo<OrderManageListResp>> orderManageList(BasePageReq pageReq ,
                                                                 @RequestParam(value = "orderStatus") String orderStatus) {

        DataValidator.isNull(pageReq,"分页实体不能为空！");
        DataValidator.isBlank(String.valueOf(orderStatus),"订单状态信息不能为空！");

        Result result = null;
            List<Integer> list = new ArrayList();
            String[] split = orderStatus.split(",");
            for (String s : split){
                list.add(Integer.parseInt(s));
            }
            result = manageOrderService.getManageList(list,pageReq,getUserId());
        return result;
    }


    /**
     * 获取当前订单管理下的服务商的账号和昵称
     *
     * @param supplierId 查找订单下的会员信息
     * @return
     */
    @GetMapping("/getSupplierMemberInfo")
    @ApiOperation(value = "获取当前订单管理下的服务商的账号和昵称", httpMethod = "GET")
    public Result<String> getSupplierMemberInfo(
            @RequestParam(value = "supplierId") Long supplierId) {

        DataValidator.isBlank(String.valueOf(supplierId),"服务商id不能为空！");

        Result result = null;
            result = manageOrderService.getSupplierMemberInfo(supplierId);
        return result;
    }

    /**
     * 获取当前订单管理下的服务商的账号和昵称
     *
     * @param purchaserId 查找订单下的会员信息
     * @return
     */
    @GetMapping("/getPurchaserInfo")
    @ApiOperation(value = "获取订单列表管理列表实体", httpMethod = "GET")
    public Result<MemberEntity> getPurchaserInfo(
            @RequestParam(value = "purchaserId") Long purchaserId) {

        DataValidator.isBlank(String.valueOf(purchaserId),"采购商id不能为空！");

        Result result = null;
            result = manageOrderService.getPurchaserInfo(purchaserId);
        return result;
    }


    /**
     * 获取当前订单管理下的服务商详情
     *
     * @param supplierId 查找订单下的会员信息
     * @return
     */
    @GetMapping("/getSupplierInfo")
    @ApiOperation(value = "获取当前订单管理下的服务商详情", httpMethod = "GET")
    public Result<SupplierEntity> getSupplierInfo(
            @RequestParam(value = "supplierId") Long supplierId) {

        DataValidator.isBlank(String.valueOf(supplierId),"服务商id不能为空！");

        Result result = null;
            result = manageOrderService.getSupplierInfo(supplierId);
        return result;
    }

    /**
     * 获取当前客户发布需求实体
     *
     * @param requirementId 查找当前客户下的发布需求信息
     * @return
     */
    @GetMapping("/orderManageRequirement")
    @ApiOperation(value = "获取当前客户发布需求实体", httpMethod = "GET")
    public Result<RequirementEntity> orderManageRequirement(
            @RequestParam(value = "requirementId") Long requirementId) {

        DataValidator.isBlank(String.valueOf(requirementId),"发布需求id不能为空！");

        Result result = null;
            result = manageOrderService.orderManageRequirement(requirementId);
        return result;
    }

    /**
     * 获取当前订单的收货信息
     *
     * @param orderId 订单id
     * @return
     */
    @GetMapping("/orderManageLogistics")
    @ApiOperation(value = "获取当前订单的收货信息", httpMethod = "GET")
    public Result<OrderLogisticsEntity> orderManageLogistics(
            @RequestParam(value = "orderId") Long orderId) {

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");

        Result result = null;
            result = manageOrderService.orderManageLogistics(orderId);
        return result;
    }


    /**
     * 获取当前订单评价信息
     *
     * @param orderId 订单id
     * @return
     */
    @GetMapping("/orderManagecomment")
    @ApiOperation(value = "获取当前订单评价信息", httpMethod = "GET")
    public Result<OrderCommentEntity> orderManagecomment(
            @RequestParam(value = "orderId") Long orderId) {

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");

        Result result = null;
            result = manageOrderService.orderManagecomment(orderId);
        return result;
    }

    /**
     * 获取当前订单详情信息
     *
     * @param orderId 订单id
     * @return
     */
    @PostMapping("/orderManageInfo")
    @ApiOperation(value = "获取当前订单详情信息", httpMethod = "POST")
    public Result orderManageInfo(
            @RequestParam(value = "orderId") Long orderId) {

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");

        Result result = null;
            result = manageOrderService.orderManageInfo(orderId);
        return result;
    }


    /**
     * 项目专员代发货，改变订单状态
     *
     * @param orderId 订单id
     * @return
     */
    @PostMapping("/userTakeSend")
    @ApiOperation(value = "项目专员代发货，改变订单状态", httpMethod = "POST")
    @OperateCut(content = "项目专员待发货")
    public Result<ResultCodeEnum> userTakeSend(
            @RequestParam(value = "orderId") Long orderId,
            @RequestBody OrderLogisticsReq orderLogisticsReq) {

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");
        DataValidator.isNull(orderLogisticsReq,"发货实体不能为空！");
        DataValidator.isBlank(String.valueOf(orderLogisticsReq.getPostCompanyId()),"快递公司id不能为空！");
        DataValidator.isBlank(String.valueOf(orderLogisticsReq.getPostCompanyName()),"快递公司名称能为空！");
        DataValidator.isBlank(String.valueOf(orderLogisticsReq.getTrackingNo()),"运单号不能为空！");
        DataValidator.isBlank(String.valueOf(orderLogisticsReq.getPostType()),"发货方式不能为空！");
        Result result = null;
            result = manageOrderService.userTakeSend(orderId,orderLogisticsReq);
        return result;
    }

    /**
     * 查看当前订单的生产进度
     *
     * @param orderId 订单id
     * @return
     */
    @GetMapping("/orderProductionLog")
    @ApiOperation(value = "查看当前订单的生产进度", httpMethod = "GET")
    @OperateCut(content = "查看当前订单的生产进度")
    public Result orderProductionLog(
            @RequestParam(value = "orderId") Long orderId) {

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");

        Result result = null;
            result = new Result( productionLogInfoMapper.selectAllProductLog(orderId));
        return result;
    }


    /**
     * 修改状态（给采购商发票，服务商给我们发票，和采购商签合同，服务商和我们签合同，来料验收，领料验收）
     *
     * @param request 订单请求实体
     * @return
     */
    @PostMapping("/editOrderOtherStatus")
    @ApiOperation(value = "修改状态（给采购商发票，服务商给我们发票，和采购商签合同，服务商和我们签合同，来料验收，领料验收）", httpMethod = "POST")
    @OperateCut(content = "修改状态，给采购商发票，服务商给我们发票，和采购商签合同，服务商和我们签合同，来料验收，领料验收")
    public Result<ResultCodeEnum> editOrderOtherStatus(@RequestBody EditOrderStatusReq request) {

        DataValidator.isNull(String.valueOf(request),"订单请求实体不能为空！");
        DataValidator.isBlank(String.valueOf(request.getId()),"订单id不能为空！！");

        Result result = null;
            result = manageOrderService.editStatus(request);
        return result;
    }
}
